Getting started with ${MODULE.NAME.PRETTY}


Prerequisites


First program: Hello world

Write a new class that extends the sws.structure.WebPage and implements a new interface:
public class Sample01HelloWorld extends WebPage implements Sample01HelloWorldFunctionList {
Make the interface contain one method:
    public void index();
Implement the method:
    @Override
    public void index() {
        HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
        htmlOutput.writeTitle("Sample 01 - Hello World - Web page");
        htmlOutput.writeHeader("Hello World", 1);
        htmlOutput.writeText("If you see this it's working.");
    }
Implement a main method:
    public static void main(String[] args) {
        SimpleWebServer.runServer(8080, new Sample01HelloWorld());
    }
The full code:
import sws.html.HtmlOutput;
import sws.server.SimpleWebServer;
import sws.structure.WebPage;

public class Sample01HelloWorld extends WebPage implements Sample01HelloWorldFunctionList {
    public static void main(String[] args) {
        SimpleWebServer.runServer(8080, new Sample01HelloWorld());
    }
    
    @Override
    public void index() {
        HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
        htmlOutput.writeTitle("Sample 01 - Hello World - Web page");
        htmlOutput.writeHeader("Hello World", 1);
        htmlOutput.writeText("If you see this it's working.");
    }
}

Run the code. It should print the following in the console:
Server is running on:
http://localhost:8080/

Browse to http://localhost:8080/
It should look as follows:

Hello World

If you see this it's working.




Definitions

Here are a few definitions that will help you to understand the more complex samples to come.

Simple Web Server
A simple web server that can run one or more web applications on one port at the same time.
It also contains general configuration (404 page, 500 page and more) for them.
Web directory
A web directory can contain zero, one or more web directories, web pages or web resources.
Web application
A Web application contains an entire web application with all the web directories, web pages, web resources and scheduled tasks in it and some general configuration (404 page, 500 page and more).
Scheduled tasks
A scheduled task defines what (Web) page function to call and when.
There are two types of scheduled tasks: Repeated (repeatedCall) and non repeated (callDelayed).
Web resource
A web resource is non HTML web resource, e.g. an image, zip, jar and other file types.
Web page
A web page contains (web) page function(s).
(Web) page function
A Java method that can generate HTML and "return" it. This method in also defined in the function list interface.
Function list
A Java interface that defines all the (Web) page function(s) in a web page.
Form object
An object set on a form tag using the htmlOutput.startForm method as default data object for the form
Data object
An object used as parameter to one of the many htmlOutput.write*InputFieldAndAssociateItWith methods to set the object on which the getter and setter should be called. This will "override" the form object.
Property list
A Java interface that defines all the getters (and thereby setters) that can be use when updating data using the HTML form tag.




Program 2: Simple link

Write a new class that extends the sws.structure.WebPage and implements a new interface:
public class Sample02SimpleLink extends WebPage implements Sample02SimpleLinkFunctionList {
Make the interface contain two methods:
    public void index();
    public void pageFunction2();
Implement the two methods:
    @Override
    public void index() {
        HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
        htmlOutput.writeTitle("Sample 02 - Simple link - Web page");
        htmlOutput.writeHeader("Simple link", 1);
        htmlOutput.writeText("This sample shows how to link to another page (Page function).");
        htmlOutput.writeBreakLine();
        htmlOutput.writeBreakLine();
        
        // This will write a link to the "pageFunction2" page function into the htmlOutput.
        // Note: The first parameter is not the class ("Sample02SimpleLink") of the page function. It is the interface (The function list) that defines the page function.
        // Note: The method pageFunction2() is not called here. It is first called when the user clicks on the link, that this code writes / generates.
        htmlOutput.writeLink(Sample02SimpleLinkFunctionList.class, "This is the label of the link").pageFunction2();
        
        htmlOutput.writeBreakLine();
    }

    @Override
    public void pageFunction2() {
        HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
        htmlOutput.writeTitle("Sample 02 - Simple link - Web page - pageFunction2");
        htmlOutput.writeHeader("Simple link", 1);
        
        htmlOutput.writeText("Congratulations you have clicked on the link.");
    }
Implement a main method:
    public static void main(String[] args) {
        SimpleWebServer.runServer(8080, new Sample02SimpleLink());
    }
For the full listing of the source code see the sws.sample.sample02simplelink package in the source code zip file.

Run the code and browse to http://localhost:8080/




Program 3: Simple object update

Same procedure as above: Write a new class with two methods defined in an interface and one main method:
import sws.html.HtmlOutput;
import sws.server.SimpleWebServer;
import sws.structure.WebPage;

public class Sample03SimpleObjectUpdateWebPage extends WebPage implements Sample03SimpleObjectUpdateWebPageFunctionList {
    public static void main(String[] args) {
        SimpleWebServer.runServer(8080, new Sample03SimpleObjectUpdateWebPage());
    }
    
    // This is the object that is updated.
    // Note: There will only be one instance of this object, which will shared between users. (It is a singleton)
    private Sample03SimpleObjectUpdateData sample03SimpleObjectUpdateData = new Sample03SimpleObjectUpdateData();

    @Override
    public void index() {
        HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
        htmlOutput.writeTitle("Sample 03 - Simple object update - Web page");
        htmlOutput.writeHeader("Simple object update", 1);
        htmlOutput.startCenter();
        
        // This will write the start of a HTML form tag into the htmlOutput.
        // The first parameter (Sample03SimpleObjectUpdateWebPageFunctionList.class) combined with the method "call" in the end (saveData) 
        // defines the submit (The "action" attribute of the HTML form tag) page function. 
        // This page function / method will not be called here (Similar to a link). It will first be called when the user clicks the submit button.
        // Note: The first parameter is not the class ("Sample03SimpleObjectUpdateWebPage") of the page function.
        // It is the interface (The function list) that defines the page function.
        // The second parameter (sample03SimpleObjectUpdateData) is the form object (This parameter is optional).
        // The form object is the object that will (as default) be updated by the form. I.e. data displayed is "get" from and updates are "set" to this object.
        htmlOutput.startForm(Sample03SimpleObjectUpdateWebPageFunctionList.class, sample03SimpleObjectUpdateData).saveData();
        
        htmlOutput.writeLabelForNextInputField("Text field ");
        
        // This will write a input tag with the type attribute set to "text" into the htmlOutput.
        // The value in this input tag field is found by calling the getTextField method on the form object (Defined by the startForm method call. See above).
        // When the user submits (Clicks the submit button) the form, the data in this field are then updated back into the form object using the setTextField method.
        // Note: The first parameter is NOT the form object nor the class of it. It is the interface that the form object implements. 
        htmlOutput.writeTextInputFieldAndAssociateItWith(Sample03SimpleObjectUpdateDataPropertyList.class).getTextField();
        
        // This will write a submit button (input tag with type attribute set to "submit") with the label "Save data" into the htmlOutput.
        htmlOutput.writeSubmitButton("Save data");
        
        htmlOutput.endForm();
        htmlOutput.endCenter();
    }

    @Override
    public void saveData() {
        HtmlOutput htmlOutput = HtmlOutput.getHtmlOutput();
        htmlOutput.writeTitle("Sample 03 - Simple object update - Web page");
        htmlOutput.writeHeader("Data saved", 1);
        htmlOutput.writeText("The text field was updated to contain: \"" + sample03SimpleObjectUpdateData.getTextField() + "\"");
        htmlOutput.writeBreakLine();
        htmlOutput.writeBreakLine();
        htmlOutput.writeLink(Sample03SimpleObjectUpdateWebPageFunctionList.class, "Back").index();
    }
}
Make note of the comments before the line:
        htmlOutput.startForm(Sample03SimpleObjectUpdateWebPageFunctionList.class, sample03SimpleObjectUpdateData).saveData();
and before this line:
        htmlOutput.writeTextInputFieldAndAssociateItWith(Sample03SimpleObjectUpdateDataPropertyList.class).getTextField();
They are important to understand (and a bit weird).

Write a very simple data class with getters and setters that implements an interface that defines the getter:
import java.io.Serializable;

public class Sample03SimpleObjectUpdateData implements Sample03SimpleObjectUpdateDataPropertyList, Serializable {
    private static final long serialVersionUID = -4323935484447061760L;
    
    private String textField;

    @Override
    public String getTextField() {
        return textField;
    }

    public void setTextField(String ptextField) {
        textField = ptextField;
    }
}

For the full listing of the source code see the sws.sample.sample03simpleobjectupdate package in the source code zip file.

Run the code and browse to http://localhost:8080/


The next programs

See the packages in the sws.sample package in the source code zip file for more sample programs.